package com.lovo.lesson1.Date;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public   class Transform{
    public static final String DEFAULT_FORAMT = "yyyy-MM-dd";
    public static final String CN_FORMAT = "yyyy-MM-dd a hh:mm:ss";
    //可以多加一个参数。定义格式
    public  static Date transformDate(String str,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = new Date();
        try {
            date = sdf.parse(str);
            return date;
        } catch (ParseException e) {
            System.out.println("错误");
            return null;
        }
    }

    public static String transformString(Date date,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String str = sdf.format(date);
        return str;
    }

    public static String transformString(Date date){
        String str = transformString(date,DEFAULT_FORAMT);
        return str;
    }

    public static String transformString(){
        String str = transformString(new Date(),CN_FORMAT);
        return str;
    }
}
